import gc

def print_memory_status():
    print(f"Memory stats before action:")
    print(f"Allocated memory: {gc.mem_alloc()} bytes")
    print(f"Free memory: {gc.mem_free()} bytes")

def demo():
    print("Starting memory status:")
    print_memory_status()

    # 禁用自动垃圾收集
    gc.disable()
    print("Garbage collection disabled.")

    # 模拟内存消耗，例如创建一个大列表然后丢弃
    big_list = [i for i in range(10000)]
    del big_list  # 这会制造一些垃圾

    # 重新启用并立即运行垃圾收集
    gc.enable()
    print("Garbage collection enabled and running collect.")
    gc.collect()

    # 设置垃圾收集阈值
    print("Setting garbage collection thresholds to (100)")
    gc.threshold(1000)

    # 再次模拟内存消耗
    another_big_list = [i for i in range(5000)]
    del another_big_list

    # 检查垃圾收集是否按阈值触发，这里只是演示设置，实际是否触发依赖具体内存变化
    print("Memory stats after creating more garbage:")
    print_memory_status()

    print("Final memory status after potential auto-collection:")
    print_memory_status()


if __name__ == '__main__':
    demo()
